/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.toedo.controllers.Controller;
import org.toedo.controllers.ui.TrayController;
import org.toedo.preferences.PreferenceConstants;
import org.toedo.utils.Constants;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize(new Point(1024, 650));
        
        configurer.setShowCoolBar(true);
        
        configurer.setShowStatusLine(true);
		configurer.setShowProgressIndicator(true);        
        
		configurer.setTitle(Constants.APP_NAME);
    }
    
    public void postWindowOpen() {
    	TrayController.getInstance().setShell(getWindowConfigurer().getWindow().getShell());
    	
    	if (Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.TRAY_SHOW_TRAY_ICON)) {
    		TrayController.getInstance().showTrayItems();
    	}
    	
    	Controller.getInstance().setStatusLineManager(getWindowConfigurer().getActionBarConfigurer().getStatusLineManager());
    }
    
    public boolean preWindowShellClose() {
    	
    	IPreferenceStore store = Activator.getDefault().getPreferenceStore();
    	if ((store.getBoolean(PreferenceConstants.TRAY_SHOW_TRAY_ICON)) &&
    			(TrayController.getInstance().isTrayShown()) &&
    			(store.getBoolean(PreferenceConstants.TRAY_MINIMIZE_ON_CLOSE))) {
    		TrayController.getInstance().hideApplication();
    		return false;
    	} else {    	
    		return true;
    	}
    }
}
